package com.tenglong.teacher.ui.homework.msgviewholder;

import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;

import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.tenglong.teacher.R;

import java.util.ArrayList;
import java.util.List;


public class HWTxtViewHolder extends HWBaseViewHolder {
    protected TextView bodyTextView;
    private String placeholder = "&/";

    public HWTxtViewHolder(View itemView) {
        super(itemView);
        bodyTextView = itemView.findViewById(R.id.nim_message_item_text_body);
    }

    public void onBindViewHolder(IMMessage msg) {
        super.onBindViewHolder(msg);
        SpannableStringBuilder text = getDisplayText(msg);
        bodyTextView.setText(text == null ? "" : text);
    }

    @Override
    protected void onItemClick(View v) {

    }

    protected SpannableStringBuilder getDisplayText(IMMessage msg) {
        if (TextUtils.isEmpty(msg.getContent())) {
            return null;
        }
        String text = msg.getContent().replace("\\n", "\n");
        List<PositionPoint> list = new ArrayList<>();
        boolean isStrat = true;
        int p = 0;
        while (isStrat) {
            int index = text.indexOf(placeholder, p);
            if (index != -1) {
                list.add(new PositionPoint(index, p = index + 4));
            } else {
                isStrat = false;
                p = 0;
            }
        }
        SpannableStringBuilder builder = new SpannableStringBuilder(text.replace(placeholder, "错误"));
        if (text.contains("错题原因：") || text.contains("作业点评：") || text.contains("书写评分：") || text.contains("老师批语：")
                || text.contains("错题原因:") || text.contains("作业点评:") || text.contains("书写评分:") || text.contains("老师批语:")) {
            if ((text.contains("错题原因：") || text.contains("错题原因:")) && !text.contains("作业点评：") && !text.contains("书写评分：") && !text.contains("老师批语：")
                    && !text.contains("作业点评:") && !text.contains("书写评分:") && !text.contains("老师批语:")) {
                bodyTextView.setTextColor(ContextCompat.getColor(mContext, R.color.mygreydark));
            } else {
                bodyTextView.setTextColor(ContextCompat.getColor(mContext, R.color.app_red));
                if (text.contains("老师批语：") || text.contains("老师批语:")) {
                    int i = 0;
                    if (text.contains("老师批语：")) {
                        i = text.indexOf("老师批语：");
                    } else if (text.contains("老师批语:")) {
                        i = text.indexOf("老师批语:");
                    }
                    if (i != 0) {
                        builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.mygreydark)), i + 5, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }
            for (PositionPoint positionPoint : list) {
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.app_red)), positionPoint.startPoint, positionPoint.stopPoint, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return getStringBuilder(builder, text, new String[]{"错题原因：", "作业点评：", "书写评分：", "老师批语：", "错题原因:", "作业点评:", "书写评分:", "老师批语:"});
        } else {
            bodyTextView.setTextColor(ContextCompat.getColor(mContext, R.color.mygreydark));
            return builder;
        }
    }

    private SpannableStringBuilder getStringBuilder(SpannableStringBuilder builder, String text, String[] tips) {
        for (String str : tips) {
            if (text.contains(str)) {
                int p = text.indexOf(str);
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.black)), p, p + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(new StyleSpan(Typeface.BOLD), p, p + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return builder;
    }

    class PositionPoint {
        int startPoint;
        int stopPoint;

        PositionPoint(int startPoint, int stopPoint) {
            this.startPoint = startPoint;
            this.stopPoint = stopPoint;
        }
    }
}
